/**
 * 
 */
package com.vision.core.cm.db.data;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.vision.core.cm.data.ActivityType;
import com.vision.core.cm.data.Domain;

/**
 * Contains information about a {@link User}'s activity.
 * 
 * @author Mark
 *
 */
@Entity
@Table(name = ActivityLog.TABLE)
public class ActivityLog<T> extends LightEntity implements Serializable {
	
	private static final long serialVersionUID = -1839081983700637541L;
	
	public static final String TABLE = "activity_log";
	
	private static final String COL_ID = "id";
	private static final String COL_USERNAME = "username";
	private static final String COL_TIMESTAMP = "timestamp";
	private static final String COL_DOMAIN = "domain";
	private static final String COL_TYPE = "type";
	private static final String COL_SERIALIZED_STRING = "serialized_string";
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "sactivity_log")
	@SequenceGenerator(name = "sactivity_log", sequenceName = "activity_log_seq")
	@Column(name = COL_ID)
	private Long id;
	
	@ManyToOne
	@JoinColumn(name = COL_USERNAME)
	private User user;
	
	@Column(name = COL_TIMESTAMP, nullable = false, updatable = false)
	private Timestamp timestamp;
	
	@Column(name = COL_DOMAIN, nullable = false, updatable = false)
	@Enumerated(EnumType.STRING)
	private Domain domain;
	
	@Column(name = COL_TYPE, nullable = false, updatable = false)
	@Enumerated(EnumType.STRING)
	private ActivityType type;
	
	@Column(name = COL_SERIALIZED_STRING, nullable = false, updatable = false, length = 5000)
	private String serializedString;
	
	// No-arg constructor for Serializable
	public ActivityLog() { }

	/**
	 * @param user
	 * @param timestamp
	 * @param domain
	 * @param type
	 * @param serializedString
	 */
	public ActivityLog(User user, Timestamp timestamp, Domain domain,
			ActivityType type, String serializedString) {
		this.user = user;
		this.timestamp = timestamp;
		this.domain = domain;
		this.type = type;
		this.serializedString = serializedString;
	}
	
	@Override
	public int hashCode() {
		return id == null ? super.hashCode() : id.hashCode();
	}
	
	@Override
	public String toString() {
		return id + ":" + timestamp + ":" + user + ":" + domain;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ActivityLog) {
			ActivityLog<?> log = (ActivityLog<?>) obj;
			return id != null && id.equals(log.id);
		}
		return false;
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Timestamp getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}

	public Domain getDomain() {
		return domain;
	}

	public void setDomain(Domain domain) {
		this.domain = domain;
	}

	public ActivityType getType() {
		return type;
	}

	public void setType(ActivityType type) {
		this.type = type;
	}

	public String getSerializedString() {
		return serializedString;
	}

	public void setSerializedString(String serializedString) {
		this.serializedString = serializedString;
	}
	
}
